package com.xajh.duoduo.impl.process;

import com.xajh.duoduo.abs.AbsProcessor;
import com.xajh.duoduo.eunm.position.*;
import com.xajh.duoduo.eunm.process.ProcessEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.awt.event.InputEvent;

/**
 * 江湖话本
 */
@Service
public class SocialDramaProcessor extends AbsProcessor implements InitializingBean {
    private final BlankPosition[] blankPositions = BlankPosition.values();
    private final FindTeamPosition[] findTeamPositions = FindTeamPosition.values();
    private final LeaveTeamPosition[] leaveTeamPositions = LeaveTeamPosition.values();
    private final SearchTeamPosition[] searchTeamPositions = SearchTeamPosition.values();
    private final SocialDramaPosition[] socialDramaPositions = SocialDramaPosition.values();
    private final AutoFindTeamPosition[] autoFindTeamPositions = AutoFindTeamPosition.values();

    @Override
    public boolean process() throws Exception {
        Robot robot = new Robot();
        robot.setAutoWaitForIdle(true);

        //点击空白防省电模式
        for (int i = 0 ; i < blankPositions.length ; i ++){
            clickBlankPosition(robot,i);
        }
        //点击组队
        for (int i = 0 ; i < blankPositions.length ; i ++){
            clickFindTeam(robot,i);
        }
        //点击离队
        for (int i = 0 ; i < blankPositions.length ; i ++){
            clickLeaveTeam(robot,i);
        }
        //点击寻找队伍
        for (int i = 0 ; i < blankPositions.length ; i ++){
            clickSearchTeam(robot,i);
        }
        //点击江湖话本
        for (int i = 0 ; i < blankPositions.length ; i ++){
            clickSocialDrama(robot,i);
        }
        //点击自动匹配
        for (int i = 0 ; i < blankPositions.length ; i ++){
            clickAutoFindTeam(robot,i);
        }

        return true;
    }

    private void clickAutoFindTeam(Robot robot, int index) {
        robot.mouseMove(autoFindTeamPositions[index].getX(), autoFindTeamPositions[index].getY());
        // 延时10毫秒
        robot.delay(10);
        // 按下鼠标左键
        robot.mousePress(InputEvent.BUTTON1_MASK);
        // 延时10毫秒
        robot.delay(10);
        // 释放鼠标左键（按下后必须要释放, 一次点击操作包含了按下和释放）
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        //防止电脑卡顿，延时0.3s
        robot.delay(300);
    }

    private void clickSocialDrama(Robot robot, int index) {
        robot.mouseMove(socialDramaPositions[index].getX(), socialDramaPositions[index].getY());
        // 延时10毫秒
        robot.delay(10);
        // 按下鼠标左键
        robot.mousePress(InputEvent.BUTTON1_MASK);
        // 延时10毫秒
        robot.delay(10);
        // 释放鼠标左键（按下后必须要释放, 一次点击操作包含了按下和释放）
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        //防止电脑卡顿，延时0.3s
        robot.delay(300);
    }

    private void clickSearchTeam(Robot robot, int index) {
        robot.mouseMove(searchTeamPositions[index].getX(), searchTeamPositions[index].getY());
        // 延时10毫秒
        robot.delay(10);
        // 按下鼠标左键
        robot.mousePress(InputEvent.BUTTON1_MASK);
        // 延时10毫秒
        robot.delay(10);
        // 释放鼠标左键（按下后必须要释放, 一次点击操作包含了按下和释放）
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        //防止电脑卡顿，延时0.3s
        robot.delay(300);
    }

    private void clickLeaveTeam(Robot robot, int index) {
        robot.mouseMove(leaveTeamPositions[index].getX(), leaveTeamPositions[index].getY());
        // 延时10毫秒
        robot.delay(10);
        // 按下鼠标左键
        robot.mousePress(InputEvent.BUTTON1_MASK);
        // 延时10毫秒
        robot.delay(10);
        // 释放鼠标左键（按下后必须要释放, 一次点击操作包含了按下和释放）
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        //防止电脑卡顿，延时0.3s
        robot.delay(300);
    }

    private void clickFindTeam(Robot robot, int index) {
        robot.mouseMove(findTeamPositions[index].getX(), findTeamPositions[index].getY());
        // 延时10毫秒
        robot.delay(10);
        // 按下鼠标左键
        robot.mousePress(InputEvent.BUTTON1_MASK);
        // 延时10毫秒
        robot.delay(10);
        // 释放鼠标左键（按下后必须要释放, 一次点击操作包含了按下和释放）
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        //防止电脑卡顿，延时0.3s
        robot.delay(300);
    }

    private void clickBlankPosition(Robot robot, int index) {
        robot.mouseMove(blankPositions[index].getX(), blankPositions[index].getY());
        // 延时10毫秒
        robot.delay(10);
        // 按下鼠标左键
        robot.mousePress(InputEvent.BUTTON1_MASK);
        // 延时10毫秒
        robot.delay(10);
        // 释放鼠标左键（按下后必须要释放, 一次点击操作包含了按下和释放）
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        //防止电脑卡顿，延时0.3s
        robot.delay(300);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        this.group = ProcessEnum.IMPEDIMENTA.getType();
        this.globleId = this.generateGlobleId();
    }
}
